﻿using System;
using System.Globalization;
using System.Security.Cryptography;
using System.Text;
using System.Text.RegularExpressions;

namespace Fly.Common.Extensions
{
    /// <summary>
    /// 字符串扩展类
    /// </summary>
    public static class StringExtensions
    {
        #region 取得MD5加密串

        /// <summary>
        /// 取得MD5加密串
        /// </summary>
        /// <param name="input">源明文字符串</param>
        /// <param name="encoding">可选的编码格式,如果为 NULL 则默认使用 UTF-8 编码</param>
        /// <returns>密文字符串</returns>
        public static string GetMd5Hash(this string input,Encoding encoding = null)
        {
            if (string.IsNullOrWhiteSpace(input))
            {
                return string.Empty;
            }
            MD5CryptoServiceProvider md5 = new MD5CryptoServiceProvider();
            if (encoding == null)
            {
                encoding = new UTF8Encoding(false);
            }
            byte[] bs = encoding.GetBytes(input);
            bs = md5.ComputeHash(bs);
            StringBuilder s = new StringBuilder();
            foreach (byte b in bs)
            {
                s.Append(b.ToString("x2").ToUpper());
            }
            return s.ToString();
        }

        #endregion

        #region 汉字转换为Unicode编码

        /// <summary>
        /// 汉字转换为Unicode编码
        /// </summary>
        /// <param name="str">要编码的汉字字符串</param>
        /// <returns>Unicode编码的的字符串</returns>
        public static string ToUnicode(string str)
        {
            byte[] bts = Encoding.Unicode.GetBytes(str);
            string r = "";
            for (int i = 0; i < bts.Length; i += 2)
                r += "&#" + bts[i + 1].ToString(CultureInfo.InvariantCulture).PadLeft(2, '0') + bts[i].ToString(CultureInfo.InvariantCulture).PadLeft(2, '0');
            return r;
        }
        #endregion

        #region 转换为 Byte 数组
        /// <summary>
        /// 将字符串按照指定的编码转换为 Byte 数组,默认使用 UTF-8 编码
        /// </summary>
        /// <param name="str">需要转换的字符串</param>
        /// <param name="encoding">编码格式</param>
        /// <returns></returns>
        public static byte[] ToBytes(this string str, Encoding encoding = null)
        {
            if (string.IsNullOrWhiteSpace(str))
            {
                return new Byte[0];
            }
            if (encoding == null)
            {
                encoding = new UTF8Encoding(false);
            }
            return encoding.GetBytes(str);
        } 
        #endregion

        #region Base64 编码
        /// <summary>
        /// 将字符串按照指定的编码方式进行 Base64 数字编码
        /// </summary>
        /// <param name="str">需要编码的字符串</param>
        /// <param name="encoding">编码方式,如果该值为 Null 则默认使用 UTF-8 编码</param>
        /// <returns></returns>
        public static string ToBase64(this string str, Encoding encoding = null)
        {
            if (string.IsNullOrWhiteSpace(str))
            {
                return string.Empty;
            }
            if (encoding == null)
            {
                encoding = new UTF8Encoding(false);
            }
            return Convert.ToBase64String(encoding.GetBytes(str));
        } 
        #endregion

        #region 使用正则表达式替换指定字符串中的字符
        /// <summary>
        /// 使用正则表达式替换指定字符串中的字符
        /// </summary>
        /// <param name="str">需要搜索的字符串</param>
        /// <param name="pattern">正则表达式</param>
        /// <param name="replacement">替换后的内容</param>
        /// <returns></returns>
        public static string RegexReplace(this string str, string pattern, string replacement = "")
        {
            if (string.IsNullOrWhiteSpace(str))
            {
                return str;
            }
            Regex regex = new Regex(pattern);
            return regex.Replace(str, replacement);
        } 
        #endregion
    }
}
